package com.hardydou.framework.filestore;

import com.hardydou.framework.filestore.AbstractFileStore.*;
import com.hardydou.framework.filestore.impl.DiskFileStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;

/**
 * @author ：hardy
 * @description：文件存储工厂类
 * @date ：Created in 2020/6/15
 * @Copyright ：Hardydou.com
 */
@Configuration
@EnableConfigurationProperties(FileStoreConfig.class)
public class FileStoreFactory implements WebMvcConfigurer {


    @Autowired
    private FileStoreConfig fileStoreConfig;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String pathPattern = fileStoreConfig.getPathPattern();
        String location = fileStoreConfig.getDiskRoot() + File.separator;
        registry.addResourceHandler(pathPattern).addResourceLocations(location);
    }

    public AbstractFileStore getFileStore(FileType fileType, BusType busType, String extName) {
        return new DiskFileStore(fileStoreConfig, fileType, busType, extName);
    }


}
